//
//  JCWCommonAlertView.swift
//  HengYouCai
//
//  Created by wll on 2017/11/17.
//  Copyright © 2017年 lwj. All rights reserved.
//  公用弹框--代码 样式--（标题、右上关闭、分割线、内容、底部按钮）

import UIKit

private typealias  callBack = (()->Void)
private typealias  clickBack = ((_ index: Int, _ btnTitle: String)->Void)


class JCWCommonAlertView: UIView {
    
    private var title: String = ""
    private var message: String = ""
    private var btnLeftTitle: String = ""
    private var btnRightTitle: String = ""
    private var sureClick: callBack?
    private var clickBack: clickBack?
    private var isCancle: Bool = false


    //MARK:构造函数

    ///弹框  title: 标题  message: 内容  btnTitle: 按钮标题 确认点击之后默认会移除弹框 双按钮 -1左 -2右
    @discardableResult convenience init( title: String?,  message: String?,  btnLeftTitle: String?,  btnRightTitle: String?,  clickBack: ((_ index: Int, _ btnTitle: String)->Void)?) {
        self.init(frame: UIScreen.main.bounds)
        self.title = title ?? ""
        self.message = message ?? ""
        self.btnLeftTitle = btnLeftTitle ?? ""
        self.btnRightTitle = btnRightTitle ?? ""
        self.clickBack = clickBack
        self.createUI()
    }
    
    /// 弹框  title: 标题  message: 内容  btnTitle: 按钮标题 确认点击之后默认会移除弹框 单按钮
    @discardableResult convenience init( title: String?,  message: String?,  btnTitle: String?,  sureClick: (()->Void)?) {
        self.init(frame: UIScreen.main.bounds)
        self.title = title ?? ""
        self.message = message ?? ""
        self.btnLeftTitle = btnTitle ?? ""
        self.sureClick = sureClick
        self.createUI()
    }
    
    /// 弹框  title: 标题  message: 内容  btnTitle: 按钮标题 确认点击之后默认会移除弹框 单按钮 isCancle: 是否有取消按钮
    @discardableResult convenience init( title: String?,  message: String?,  btnTitle: String?,  sureClick: (()->Void)?, isCancle: Bool?) {
        self.init(frame: UIScreen.main.bounds)
        self.title = title ?? ""
        self.message = message ?? ""
        self.btnLeftTitle = btnTitle ?? ""
        self.sureClick = sureClick
        self.isCancle = isCancle ?? false
        self.createUI()
    }

    //MARK:添加UI
    private func createUI(){
        //所有内容都为空的时候直接返回
        if self.title.isEmpty && self.message.isEmpty && self.btnLeftTitle.isEmpty && self.btnRightTitle.isEmpty{
            return
        }
        self.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        UIApplication.shared.keyWindow?.addSubview(self)
        self.addSubview(self.cv)
        if self.isCancle == false {
            self.cv.addSubview(self.closeBtn)
        }
        if !self.title.isEmpty {
            self.cv.addSubview(self.titleLab)
            self.titleLab.text = self.title
            self.cv.addSubview(self.sepLine)
        }
        if !self.message.isEmpty {
            self.cv.addSubview(self.messageLab)
            self.messageLab.text = self.message
        }
        if !self.btnLeftTitle.isEmpty {
            self.cv.addSubview(self.bottomLeftBtn)
            self.bottomLeftBtn.setTitle(self.btnLeftTitle, for: .normal)
            if !self.btnRightTitle.isEmpty {
                self.cv.addSubview(self.bottomRightBtn)
                self.bottomRightBtn.setTitle(self.btnRightTitle, for: .normal)
                //改变左边按钮样式
                self.bottomLeftBtn.setTitleColor(Color.black, for: .normal)
                self.bottomLeftBtn.backgroundColor = UIColor.white
                self.bottomLeftBtn.layer.borderColor = Normal_ThemeColor.cgColor
                self.bottomLeftBtn.layer.borderWidth = 0.6
            }else{
                 self.bottomLeftBtn.backgroundColor = Normal_ThemeColor
                
            }
        }

        self.cv.bringSubview(toFront: closeBtn)
    }
    //MARK:布局
    override func layoutSubviews() {
        super.layoutSubviews()
        let witdth = self.frame.size.width - 70
        var height: CGFloat = 0
        if !self.title.isEmpty {
            self.titleLab.frame = CGRect(x: 0, y: 0, width: witdth, height: 45)
            height += 45
        }
        if self.closeBtn.isDescendant(of: self.cv), self.isCancle == false {
            self.closeBtn.frame = CGRect(x: witdth-45, y: 0, width: 45, height: 45)
        }
        if self.sepLine.isDescendant(of: self.cv) {
            height += 0.5
            self.sepLine.frame = CGRect(x: 0, y: height, width: witdth, height: 0.5)
        }
        if !self.message.isEmpty {
            height += 23//margin
            //sizeToFit计算高度 要先给一个高度 才能计算 否则按照1行显示高度
            self.messageLab.frame = CGRect(x: 25, y: height, width: 0, height: 0)
            self.messageLab.sizeToFit()
            self.messageLab.textAlignment = (self.messageLab.width_sd > witdth - 50) ? .left : .center
            self.messageLab.frame = CGRect(x: 25, y: height, width: witdth-50, height: 0)
            self.messageLab.sizeToFit()
            let labH = self.messageLab.frame.size.height + 0.1
            self.messageLab.frame = CGRect(x: 25, y: height, width: witdth-50, height: labH)
            height += labH
            height += 23//margin
        }
        if !self.btnLeftTitle.isEmpty {
            self.bottomLeftBtn.frame = CGRect(x: 25, y: height, width: witdth-50, height: 36)
            if !self.btnRightTitle.isEmpty {
                let btnSpace: CGFloat = 15
                let btnW: CGFloat = (witdth-50-btnSpace)/2
                self.bottomLeftBtn.frame = CGRect(x: 25, y: height, width: btnW, height: 36)
                self.bottomRightBtn.frame = CGRect(x: 25+btnW+btnSpace, y: height, width: btnW, height: 36)
            }
            height += 36
            height += 22.5//margin
        }

        self.cv.bounds = CGRect(x: 0, y: 0, width: witdth, height: height)
        self.cv.center = self.center
    }
    //MARK:UI
    ///中间白框
    lazy var cv: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor.white
        temp.layer.cornerRadius = 10
        return temp
    }()
    ///华丽的分割线
    lazy var sepLine:UIView = {
        let temp = UIView()
        temp.backgroundColor = Normal_CCCColor
        return temp
    }()
    ///右上角关闭按钮
    lazy var closeBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.setImage(UIImage(named: "calcu_close"), for: .normal)
        temp.addTarget(self, action: #selector(closeBtnClick), for: .touchUpInside)
        return temp
    }()
    ///标题
    lazy var titleLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.font = BoldFont_WL(16)
        temp.textColor = Normal_333Color
        return temp
    }()
    ///内容
    lazy var messageLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = Normal_333Color
        temp.numberOfLines = 0
        temp.font = Font_WL(14)
        return temp
    }()
    ///按钮左边底部
    lazy var bottomLeftBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.backgroundColor = Color.white
        temp.titleLabel?.textColor = Color.black
        temp.titleLabel?.font = Font_WL(14)
        temp.layer.cornerRadius = 5
        temp.addTarget(self, action: #selector(bottomBtnClick), for: .touchUpInside)
        temp.tag = -1;
        return temp
    }()
    ///按钮右边底部
    lazy var bottomRightBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.backgroundColor = Normal_ThemeColor //Normal_RedColor
        temp.titleLabel?.textColor =   Color.black //UIColor.white
        temp.titleLabel?.font = Font_WL(14)
        temp.layer.cornerRadius = 5
        temp.addTarget(self, action: #selector(bottomBtnClick), for: .touchUpInside)
        temp.tag = -2;
        return temp
    }()
    
    
    //MARK:响应事件
    func bottomBtnClick(sender: UIButton) {
        self.sureClick?()
        self.clickBack?(sender.tag, sender.currentTitle ?? "")
        self.removeFromSuperview()
    }
    
    func closeBtnClick() {
        self.removeFromSuperview()
    }
}
